from rest_framework.generics import CreateAPIView,GenericAPIView
from rest_framework.viewsets import GenericViewSet
from sys_manager.models import UserModel
from  rest_framework import  mixins

from sys_manager.serializer_kit.user_serializer import UserAddSerializer, UpdateOrDestroySerializer, RetrieveSerilizer

from sys_manager.serializer_kit.user_serializer import ResetPasswordSerilizer
from ERP_system.utils.multiple_destory import MultipleDestoryMixin
from ERP_system.utils.pagination import GlobalPagination
from rest_framework.decorators import action

class  RegisterView(CreateAPIView):


    queryset = UserModel.objects.all()
    serializer_class = UserAddSerializer


#除了用户添加和密码修改以外，其他的像修改部门，修改角色，删除部门，删除角色等操作
class UserView(mixins.RetrieveModelMixin,
                mixins.UpdateModelMixin,
                mixins.DestroyModelMixin,
                mixins.ListModelMixin,
                MultipleDestoryMixin,
                GenericViewSet):


    queryset = UserModel.objects.all()
    pagination_class = GlobalPagination

    def get_serializer_class(self):
        #如果是修改和删除的操作，使用一个统一的序列化
        if self.action=='partial_update' or  self.action=='update' or  self.action=='destroy':
              return UpdateOrDestroySerializer

        else:
            return RetrieveSerilizer

class  ResetPasswordView(mixins.UpdateModelMixin,GenericAPIView):
    queryset = UserModel.objects.all()
    serializer_class = ResetPasswordSerilizer

    def patch(self,request,*args,**kwargs):
        return self.partial_update(request,*args,**kwargs)